package com.knife.hospital.util.order;

import com.knife.hospital.entity.DepartmentPO;
import com.knife.hospital.entity.DoctorPO;
import com.knife.hospital.entity.OrderPO;
import com.knife.hospital.mapper.DepartmentPOMapper;
import com.knife.hospital.mapper.DoctorPOMapper;
import com.knife.hospital.model.order.OrderRequest;
import com.knife.hospital.model.order.UserOrderResponse;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Objects;

/**
 * @author Jihuai
 */
@Component
public class UserOrderUtil {
    @Resource
    private DepartmentPOMapper departmentPOMapper;
    @Resource
    private DoctorPOMapper doctorPOMapper;

    public UserOrderResponse orderPoToUserOrderResponse(OrderPO orderPO) {
        if (Objects.isNull(orderPO))
            return null;
        UserOrderResponse userOrderResponse = new UserOrderResponse();

        DoctorPO doctorPO = doctorPOMapper.selectByPrimaryKey(orderPO.getDoctorId());
        DepartmentPO departmentPO = departmentPOMapper.selectByPrimaryKey(doctorPO.getDepartmentId());

        userOrderResponse.setId(orderPO.getId());
        userOrderResponse.setPatientId(orderPO.getPatientId());
        userOrderResponse.setDepartmentId(departmentPO.getId());
        userOrderResponse.setDepartmentName(departmentPO.getName());
        userOrderResponse.setDoctorId(doctorPO.getId());
        userOrderResponse.setDoctorName(doctorPO.getName());
        userOrderResponse.setDate(orderPO.getDate());
        userOrderResponse.setTimeQuantum(orderPO.getTimeQuantum());
        userOrderResponse.setOrderTime(orderPO.getOrderTime());
        userOrderResponse.setStatement(orderPO.getStatement());
        userOrderResponse.setRank(orderPO.getRank());
        userOrderResponse.setComment(orderPO.getComment());
        userOrderResponse.setReserved(orderPO.getReserved());
        return userOrderResponse;
    }

    public OrderPO OrderRequestToOrderPo(OrderRequest orderRequest) {
        OrderPO orderPO = new OrderPO();
        orderPO.setPatientId(orderRequest.getPatientId());
        orderPO.setDoctorId(orderRequest.getDoctorId());
        orderPO.setDate(orderRequest.getDate());
        orderPO.setTimeQuantum(orderRequest.getTimeQuantum());
        // 因为OrderRequest只用来申请预约
        orderPO.setOrderTime(new Date());
        // todo 目前这里固定写法,之后需要使用Enum常量
        orderPO.setStatement(2);
        // todo rank的规范之后具体制定，目前依然是固定写法
        orderPO.setRank(-1);
        return orderPO;
    }
}
